package com.android.neardi.framework.apitest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.EthernetManager;
import android.os.Build;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.android.neardi.framework.apitest.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    private Context mContext;

    //Test Ethernet Api
    private EthernetManager mEthManager;
    private String mIfaceName;

    /**
     * See LKD3568-SDK-Android-V1.0/frameworks/base/core/java/android/content/Context.java
     */
    public static final String ETHERNET_SERVICE = "ethernet";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.toolbar);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        testEthApi();
    }

    @SuppressLint("WrongConstant")
    private void testEthApi() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mEthManager = (EthernetManager) getSystemService(ETHERNET_SERVICE);
            //See LKD3568-SDK-Android-V1.0/packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java
            if (mEthManager == null) {
                Log.e(TAG, "get ethernet manager failed");
                Toast.makeText(mContext, R.string.disabled_feature, Toast.LENGTH_SHORT).show();
                return;
            }
            String[] ifaces = mEthManager.getAvailableInterfaces();
            if (ifaces.length > 0) {
                mIfaceName = ifaces[0];//"eth0";
                String ifaceNameDebug = String.format("mIfaceName:%s ", mIfaceName);
                Log.d(TAG, ifaceNameDebug);
                Toast.makeText(mContext, ifaceNameDebug, Toast.LENGTH_SHORT).show();
            }
            if (null == mIfaceName) {
                Log.e(TAG, "get ethernet ifaceName failed");
                Toast.makeText(mContext, R.string.disabled_feature, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}